from pyhive import hive
from pymysql import *

conn = Connection(host='localhost',user='root',password='qinxiao123456',database='bigdata',port=3306)

# 创建 Hive 连接池
# hive_pool = PooledDB(
#     creator=hive.Connection,  # 使用 pyhive 的 hive.Connection 作为连接创建器
#     host='master',
#     port=10000,
#     username='qx',
#     maxconnections=10,  # 最大连接数
# )

# 使用连接池管理 Hive 连接
def queryhive(sql, params, query_type='no_select'):
    # 从连接池中获取连接
    # conn = hive_pool.connection()
    cursor = conn.cursor()
    params = tuple(params)
    print(sql)
    cursor.execute(sql)
    if query_type != 'no_select':
        data_list = cursor.fetchall()
        conn.commit()
        # cursor.close()
        # conn.close()  # 关闭连接返回到连接池
        return data_list
    else:
        conn.commit()
        # cursor.close()
        # conn.close()  # 执行完后立即关闭连接
        return '数据库语句执行成功'


if __name__ == '__main__':
    data = queryhive('select * from catMovieData',[],'select')[:1]
    print(data)



# from pyhive import hive
#
# conn = hive.Connection(host='node1',port=10000,username='hadoop')
#
# cusror = conn.cursor()
#
#
# def queryhive(sql,params,type='no_select'):
#     params = tuple(params)
#     cusror.execute(sql,params)
#
#     if type != 'no_select':
#         data_list = cusror.fetchall()
#         conn.commit()
#         return data_list
#     else:
#         conn.commit()
#         return '数据库语句执行成功'

# data = queryhive('select * from catMovieData',[],'select')
#
# print(data[0])